package com.woniuxy.booth.mapper;

import com.woniuxy.booth.bean.AdminBean;
import com.woniuxy.booth.bean.DeptBean;
import com.woniuxy.booth.bean.RoleBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

/**
 * @author linke
 * @version 1.0
 * @className: AdminMapper
 * @description: 管理员持久层接口
 * @date 2021/11/15 1:16
 */
public interface AdminMapper {
    /**
     * 新增管理员
     * @param adminBean
     * @return 受影响的行数
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "a.id"
    )
    @Insert("insert into admin_info " +
            "(admin_name,login_name,password,telphone,create_time,status,fk_dept_id,fk_role_id)" +
            " values(#{a.adminName},#{a.loginName},#{a.password},#{a.telphone},now(),'0',#{a.deptBean.id}," +
            "#{a.roleBean.id})")
    int saveAdminBean(@Param("a") AdminBean adminBean);


    /**
     * 修改管理员
     * @param adminBean
     * @return 受影响的行数
     */
    @Update("<script>" +
            "update admin_info" +
            "<set>" +
            "<if test='a.adminName != null'>" +
            " admin_name = #{a.adminName}," +
            "</if>" +
            "<if test='a.loginName != null'>" +
            " login_name = #{a.loginName}," +
            "</if>" +
            "<if test='a.password != null'>" +
            " password = #{a.password}," +
            "</if>" +
            "<if test='a.telphone != null'>" +
            " telphone = #{a.telphone}," +
            "</if>" +
            "<if test='a.status != null'>" +
            " status = #{a.status}," +
            "</if>" +
            "<if test='a.deptBean.id != null'>" +
            " fk_dept_id = #{a.deptBean.id}," +
            "</if>" +
            "<if test='a.roleBean.id != null'>" +
            " fk_role_id = #{a.roleBean.id}," +
            "</if>" +
            "</set>" +
            "where id = #{a.id}" +
            "</script>")
    int updateAdminBean(@Param("a") AdminBean adminBean);
    /**
     * 删除管理员
     * @param adminBean
     * @return 受影响的行数
     */

    @Delete("delete from admin_info where id = #{a.id}")
    int deleteAdminBean(@Param("a")AdminBean adminBean);
    /**
     * 根据ID查询管理员
     * @param id
     * @return
     */

    @Results({
            @Result(id=true,property = "id",column = "id",javaType = Long.class),
            @Result(property = "adminName",column = "admin_name",javaType = String.class),
            @Result(property = "loginName",column = "login_name",javaType = String.class),
            @Result(property = "password",column = "password",javaType = String.class),
            @Result(property = "deptBean",column = "fk_dept_id",javaType = DeptBean.class,one =
            @One(select = "com.woniuxy.ssm.mapper.DeptMapper.getOneById",fetchType = FetchType.LAZY)),
            @Result(property = "roleBean",column = "fk_role_id",javaType = RoleBean.class,one =
            @One(select = "com.woniuxy.ssm.mapper.RoleMapper.getOneById",fetchType = FetchType.LAZY))
    })
    @Select("select * from admin_info where id = #{id}")
    AdminBean getOneById(Long id);

    //分页查询
}
